<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:xdb="http://orbeon.org/oxf/xml/xmldb" xmlns:xu="http://www.xmldb.org/xupdate">

	<p:processor name="oxf:request">
		<p:input name="config">
			<config >
				<include>/request/parameters</include>
			</config>
		</p:input>
		<p:output name="data" id="request"/>
	</p:processor>

	<p:processor name="oxf:pipeline">
		<p:input name="config" href="../session.xpl"/>
		<p:output name="data" id="session"/>
	</p:processor>

	<p:processor name="oxf:xslt">
		<p:input name="data" href="#request#xpointer(/request/parameters)"/>
		<p:output name="data" id="action"/>
		<p:input name="config">
			<xsl:stylesheet version="2.0">
				<xsl:template match="/">
					<xsl:element name="{/parameters/parameter[name = 'action']/value}">
						<xsl:apply-templates select="/parameters/parameter"/>
					</xsl:element>
				</xsl:template>
				<xsl:template match="parameter">
					<xsl:if test="name != 'action'">
						<xsl:for-each select="value">
							<xsl:element name="{replace(../name, '\[|\]', '')}">
								<xsl:value-of select="."/>
							</xsl:element>
						</xsl:for-each>
					</xsl:if>
				</xsl:template>
			</xsl:stylesheet>
		</p:input>
	</p:processor>
	<p:processor name="oxf:xslt">
		<p:input name="data" href="#session"/>
		<p:output name="date" id="context"/>
		<p:input name="config">
			<context xsl:version="2.0">
				<xsl:choose>
					<xsl:when test="/session/groups/@current = 'gr_0'">/users/user[@id = '<xsl:value-of select="/session/username"/>']</xsl:when>
					<xsl:otherwise>/groups/group[@id='<xsl:value-of select="/session/groups/@current"/>']</xsl:otherwise>
				</xsl:choose>
			</context>
		</p:input>
	</p:processor>

	<p:choose href="#action">
		<p:when test="name(/*) = 'add'">
			<p:processor name="oxf:xslt">
				<p:input name="data" href="#action"/>
				<p:input name="context" href="#context"/>
				<p:output name="data" id="query"/>
				<p:input name="config">
					<xdb:update xsl:version="2.0" collection="/db/portal">
						<xsl:variable name="col" select="name(/add/*[text() = concat( 'widgetContainer_',/add/widgetId)])"/>
						<xu:modifications version="1.0">
							<xu:append select="{doc('input:context')/context/text()}/interface">
								<widget id="{/add/widgetId}">
									<column>
										<xsl:value-of select="string($col)"/>
									</column>
									<minimized>0</minimized>
									<position>
										<xsl:for-each select="/add/*[name(.) = $col]">
											<xsl:if test="text() = concat('widgetContainer_',/add/widgetId)">
												<xsl:value-of select="position()"/>
											</xsl:if>
										</xsl:for-each>
									</position>
								</widget>
							</xu:append>
							<xsl:for-each select="/add/*[name(.) = $col]">
								<xsl:if test=". != concat('widgetContainer_', /add/widgetId)">
									<xu:update select="{doc('input:context')/context/text()}/interface/widget[@id = '{substring-after(., '_')}']/position">
										<xsl:value-of select="position()"/>
									</xu:update>
								</xsl:if>
							</xsl:for-each>
						</xu:modifications>
					</xdb:update>
				</p:input>
			</p:processor>
		</p:when>
		<p:when test="name(/*) = 'delete'">
			<p:processor name="oxf:xslt">
				<p:input name="data" href="#action"/>
				<p:input name="context" href="#context"/>
				<p:output name="data" id="query"/>
				<p:input name="config">
					<xdb:update xsl:version="2.0" collection="/db/portal">
						<xsl:variable name="col" select="name(/delete/*[text() = concat( 'widgetContainer_',/delete/widgetId)])"/>
						<xu:modifications version="1.0">
							<xu:remove select="{doc('input:context')/context/text()}/interface/widget[@id = '{/delete/widgetId}']"/>
							<xsl:for-each select="/delete/*[name(.) = $col]">
								<xsl:if test=". != concat('widgetContainer_', /delete/widgetId)">
									<xu:update select="{doc('input:context')/context/text()}/interface/widget[@id = '{substring-after(., '_')}']/position">
										<xsl:value-of select="position() -1"/>
									</xu:update>
								</xsl:if>
							</xsl:for-each>
						</xu:modifications>
					</xdb:update>
				</p:input>
			</p:processor>
		</p:when>
		<p:when test="name(/*) = 'minimize'">
			<p:processor name="oxf:xslt">
				<p:input name="data" href="#action"/>
				<p:input name="context" href="#context"/>
				<p:output name="data" id="query"/>
				<p:input name="config">
					<xdb:update xsl:version="2.0" collection="/db/portal">
						<xu:modifications version="1.0">
							<xu:variable name="tget" select="{doc('input:context')/context/text()}/interface/widget[@id = '{/minimize/widgetId}']/minimized"/>
							<xu:update select="$tget">
								<xu:value-of select="(number($tget) + 1) mod 2"/>
							</xu:update>
						</xu:modifications>
					</xdb:update>
				</p:input>
			</p:processor>
		</p:when>
		<p:when test="name(/*) = 'move'">
			<p:processor name="oxf:xslt">
				<p:input name="data" href="#action"/>
				<p:input name="context" href="#context"/>
				<p:output name="data" id="query"/>
				<p:input name="config">
					<xdb:update xsl:version="2.0" collection="/db/portal">
						<xu:modifications version="1.0">
							<xsl:for-each select="/move/*[substring-before(., '_') = 'widgetContainer']">
								<xsl:variable name="tget" select="."/>
								<xu:update select="{doc('input:context')/context/text()}/interface/widget[@id = '{substring-after(., '_')}']/position">
									<xsl:value-of select="index-of(/move/*[name(.) = name($tget)], $tget)"/>
								</xu:update>
								<xu:update select="{doc('input:context')/context/text()}/interface/widget[@id = '{substring-after(., '_')}']/column">
									<xsl:if test=". = $tget">
										<xsl:value-of select="name($tget)"/>
									</xsl:if>
								</xu:update>
							</xsl:for-each>
						</xu:modifications>
					</xdb:update>
				</p:input>
			</p:processor>
		</p:when>
		<p:otherwise>
			<p:processor name="oxf:xslt">
				<p:input name="data" href="#action"/>
				<p:output name="data" id="query"/>
				<p:input name="config">
					<xdb:update xsl:version="2.0" collection="/db/portal">
						<xu:modifications version="1.0"/>
					</xdb:update>
				</p:input>
			</p:processor>
		</p:otherwise>
	</p:choose>

	<p:processor name="oxf:xmldb-update">
		<p:input name="datasource" href="/config/datasource.xml"/>
		<p:input name="query" href="#query"/>
	</p:processor>

	<!--<p:processor name="oxf:xml-converter">
		<p:input name="config">
			<config>
				<content-type>text/xml</content-type>
				<encoding>utf-8</encoding>
				<version>1.0</version>
				<omit-xml-declaration>true</omit-xml-declaration>
			</config>
		</p:input>
		<p:input name="data" href="#query"/>
		<p:output name="data" id="xml-doc"/>
	</p:processor>

	<p:processor name="oxf:http-serializer">
		<p:input name="config">
			<config>
				<content-type>text/xml</content-type>
				<force-content-type>true</force-content-type>
			</config>
		</p:input>
		<p:input name="data" href="#xml-doc"/>
	</p:processor>-->

</p:config>